package com.ebspp.server;

import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;

import com.ebspp.global.General;
import com.ebspp.thrift.DataRead;

/**
 * Server for supporting read, based on threadPool
 * Note that if a client comes to read, it can preempt other client's resources. No rejection.
 * The server should try its best to avoid such condition by
 * 1. Client will provision resource before read.
 * 2. If no resource available, refers to another node to which the data on this node is replicated to.
 * 
 * @author x23pan
 *
 */
public class EBSPPReadServer extends EBSPPServer
{
		public static void initServer()
		{	 
			try 
			{
				TServerSocket serverTransport = new TServerSocket(General.rPort);
				
				DataRead.Processor processor = new DataRead.Processor(new ReadHandler());
	
				TServer server = new TThreadPoolServer(
									new TThreadPoolServer.Args(serverTransport).processor(processor));
				/*
				TServer server = new TThreadPoolServer(
						new TThreadPoolServer.Args(serverTransport).processor(processor).
						protocolFactory(new TBinaryProtocol.Factory(true, true)));*/
				
				System.out.println("Start read server at port: " + General.rPort);
				
				server.serve();
				System.err.println("Read server closed");
			} catch (TTransportException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	
}
